#include "clientsocket.h"

ClientSocket::ClientSocket(QObject *parent) :
    QTcpSocket(parent)
{
   connect(this,SIGNAL(connected()),this,SLOT(slotConnect()));
   connect(this,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
   connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(Error(QAbstractSocket::SocketError)));
   Server_exist=false;
}

void ClientSocket::slotConnect()
{

    write(DataReceived);
    waitForBytesWritten();//必须加，等待字节发送成功
    disconnectFromHost();
    emit error("Success\r\n");
    emit success();
}

void ClientSocket::slotDisconnected()
{
    // close();
}
void ClientSocket::HostFound()
{
}

void ClientSocket::SetIp(QString ip)
{
    address.setAddress(ip);
}
void ClientSocket::SetPort(int pt)
{
    port = pt;
}

void ClientSocket::SendGps(QString gps)
{
    QByteArray tmp(gps.toUtf8());
    DataReceived = tmp;
    connectToHost(address,port);
}

void ClientSocket::Error(QAbstractSocket::SocketError socketerror)
{

    switch (socketerror)
    {
        case UnknownSocketError:
                emit error("An unidentified error occurred.\r\n");
                break;
        case ConnectionRefusedError:
                emit error("The connection was refused by the peer (or timed out).\r\n");
                break;
        case RemoteHostClosedError:
                emit error("The remote host closed the connection. Note that the client socket\r\n");
                break;
        case HostNotFoundError:
                 emit error("The host address was not found.\r\n");
                break;
        case SocketAccessError:
                 emit error("The socket operation failed because the application lacked the required privileges\r\n");
                break;
        case SocketResourceError:
                 emit error("The local system ran out of resources (e.g., too many sockets).\r\n");
                break;
        case SocketTimeoutError:
                 emit error("The socket operation timed out.\r\n");
                break;
        case DatagramTooLargeError:
                 emit error("The datagram was larger than the operating system's limit\r\n");
                break;
        case NetworkError:
                emit error("An error occurred with the network\r\n");
                break;
        case AddressInUseError:
                 emit error("The address specified to QUdpSocket::bind() is already in use and was set to be exclusive.\r\n");
                break;
        case 9:
                 emit error("error9\r\n");
                break;
        case 10:
                 emit error("error10\r\n");
                break;
        case 11:
                 emit error("error11\r\n");
                break;
        case ProxyAuthenticationRequiredError:
                 emit error("The socket is using a proxy, and the proxy requires authentication.\r\n");
                break;
        case SslHandshakeFailedError:
                 emit error("The SSL/TLS handshake failed, so the connection was closed (only used in QSslSocket)\r\n");
                break;
        case ProxyConnectionRefusedError:
                 emit error("Could not contact the proxy server because the connection to that server was denied\r\n");
                break;
        case ProxyConnectionClosedError:
                 emit error("he connection to the proxy server was closed unexpectedly (before the connection to the final peer was established)\r\n");
                break;

        case ProxyConnectionTimeoutError:
                 emit error("The connection to the proxy server timed out or the proxy server stopped responding in the authentication phase\r\n");
                break;
        case ProxyNotFoundError:
                 emit error("The proxy address set with setProxy() (or the application proxy) was not found.\r\n");
                break;
        case ProxyProtocolError:
                 emit error("The connection negotiation with the proxy server because the response from the proxy server could not be understood.\r\n");
                break;

            }
}
